package com.zt.student.service.impl;

import com.zt.common.pojo.homework.Homework;
import com.zt.student.dto.HomeworkWithClassDTO;
import com.zt.common.utils.JwtUtil;
import com.zt.student.dao.HomeworkDao;
import com.zt.student.dto.HomeworkDTO;
import com.zt.student.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2022-02-12
 */
@Service
@Transactional
public class HomeworkServiceImpl implements HomeworkService {

    @Autowired
    private HomeworkDao homeworkDao;

    @Override
    public List<HomeworkDTO> getByClassId(HttpServletRequest request, int classId) {
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        List<HomeworkDTO> homeworkDTOS = homeworkDao.getByClassId(classId, studentId);
        homeworkDTOS.forEach(h -> h.setSubmitted(h.getHomeworkAnswer().getAnswerId()!=0));

        return homeworkDTOS;
    }

    /*
     * @param request 学号
     * @param semester 学期
     * @return 获取该学生该学期的所有作业情况
     */
    @Override
    public List<HomeworkWithClassDTO> getBySemester(HttpServletRequest request, String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));
        List<HomeworkWithClassDTO> homeworkWithClassDTOS = homeworkDao.getBySemester(semester, studentId);
        homeworkWithClassDTOS.forEach(h -> h.setSubmitted(h.getHomeworkAnswer().getAnswerId()!=0));

        return homeworkWithClassDTOS;
    }

    @Override
    public Homework getByHomeworkId(int homeworkId){
        return homeworkDao.getByHomeworkId(homeworkId);
    }
}
